<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-date-picker v-model="startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="gotoback">返回</el-button>
        <!--  <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>

    </el-form>
<!-- 	<div style="color:#909399;font-weight: bold;margin-bottom: 10px;">
		设备在线状态:正常&nbsp;&nbsp;&nbsp;&nbsp;设备离线时间：2021-08-14 08:12:16;
	</div> -->
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <el-table-column prop="index" header-align="center" align="center" label="序号">
      </el-table-column>
      <el-table-column prop="equId" header-align="center" align="center" label="设备识别码">
      </el-table-column>
      <el-table-column prop="runTime" header-align="center" align="center" label="设备运行时间戳">
      </el-table-column>
      <el-table-column prop="longitude" header-align="center" align="center" label="经度">
      </el-table-column>
      <el-table-column prop="latitude" header-align="center" align="center" label="纬度">
      </el-table-column>
      <el-table-column prop="equStatus" header-align="center" align="center" label="设备状态">
      </el-table-column>
      <el-table-column prop="equSteps" header-align="center" align="center" label="设备步数">
      </el-table-column>
      <el-table-column prop="equElectric" header-align="center" align="center" label="设备电量">
      </el-table-column>
      <el-table-column prop="equTemperature" header-align="center" align="center" label="设备温度">
      </el-table-column>
      <el-table-column prop="equVoltage" header-align="center" align="center" label="设备电压">
      </el-table-column>
      <el-table-column prop="equCurrent" header-align="center" align="center" label="电流">
      </el-table-column>
	  <el-table-column prop="mah" header-align="center" align="center" label="电池容量">
	       </el-table-column>
	  <el-table-column prop="ip" header-align="center" align="center" label="设备IP">
	       </el-table-column>
	       <el-table-column prop="heardOrder" header-align="center" align="center" label="包头指令">
	       </el-table-column>
      <!-- <el-table-column
        prop="variableLength"
        header-align="center"
        align="center"
        label="不定长数据">
      </el-table-column> -->
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
      </el-table-column>
      <!-- <el-table-column
        prop="allMessage"
        header-align="center"
        align="center"
        label="所有报文信息">
      </el-table-column> -->
      <!-- <el-table-column
        prop="tableName"
        header-align="center"
        align="center"
        label="表名">
      </el-table-column>
      <el-table-column
        prop="remainder"
        header-align="center"
        align="center"
        label="%6000余数"> -->
      </el-table-column>
      <!-- <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './messlog-add.vue'
  export default {
    data() {
      return {

        dataForm: {
          key: ''
        },
        startTime: '',
        endTime: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        equNumber: null,
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      // var equNumber = ''
      if (this.$route.query.equNumber) {
        this.equNumber = this.$route.query.equNumber
      }
      // this.getDataList(id)
      this.getDataList()
    },
    methods: {
		gotoback(){
			this.$router.back(-1)
		},
      // 获取数据列表
      getDataList(equNumber) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/messagelog/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            // 'key': this.dataForm.key,
            equNumber: this.equNumber,
            startTime: this.startTime,
            endTime: this.endTime,
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
            console.log(this.dataList)
            for (var i = 0; i < this.dataList.length; i++) {
              this.dataList[i].index = i + 1
            }
            // for (var i in this.dataList) {
            //   if (this.dataList[i].equStatus == 1) {
            //     this.dataList[i].equStatus = "异常"
            //   }
            //   if (this.dataList[i].equStatus == 0) {
            //     this.dataList[i].equStatus = "正常"
            //   }
            // }

          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/message1020321081613202108/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
